import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.InfModel;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasoner;
import com.hp.hpl.jena.reasoner.rulesys.Rule;


public class TestReasonRule {
	// Reasoner reasoner;
	GenericRuleReasoner reasoner;
	
	Model model;

	Resource configuration;

	InfModel infModel;

	public TestReasonRule() {
		this.reasoner = null;
		// this.model = null;
		this.configuration = null;
		this.infModel = null;
	}

	public OntModel runEngine(String ruleFile, Model model) throws IOException {
		// model = ModelFactory.createDefaultModel();

		// configuration = model.createResource();
		//
		// configuration.addProperty(ReasonerVocabulary.PROPruleMode, "hybrid");
		//
		// configuration.addProperty(ReasonerVocabulary.PROPruleSet, ruleFile);
		//
		//
		// reasoner =
		// GenericRuleReasonerFactory.theInstance().create(configuration);

		List rules = Rule.rulesFromURL(ruleFile);
		reasoner = new GenericRuleReasoner(rules);
		reasoner.setOWLTranslation(true);
		reasoner.setTransitiveClosureCaching(true);

		// model = FileManager.get().loadModel(rdfFile);

		infModel = ModelFactory.createInfModel(reasoner, model);

		infModel.prepare();
		// infModel.getRawModel();
		// infModel.write(System.out);

		try {
			// FileInputStream fin =new FileInputStream(rdfFile);
			FileOutputStream fout = new FileOutputStream("testResults3.owl");
			infModel.write(fout);
		} catch (IOException e) {
			System.out.println("Exception caught" + e.getMessage());
		}

		 OntModel infOntModel = ModelFactory.createOntologyModel();
		 File infFile = new File("testResults3.owl");
		 //infOntModel.read(infModel.toString());
		 infOntModel.read(new FileInputStream(infFile), "");
		
		 return infOntModel;
	}

	public String chooseReasonRule(String queryKeyWord) {

		String nameRuleFile;

		nameRuleFile = "smartspace" + queryKeyWord + ".rules";

		return nameRuleFile;

	}
}
